package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.application.query.vo.SafetyCheckVo;
import com.smedi.ismedi.inmail.core.domain.dto.SafetyCheckQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.SafetyCheckRemoteApi;
import com.smedi.ismedi.safetycheck.dto.request.check.SafetyCheckListRequest;
import com.smedi.ismedi.safetycheck.dto.request.check.SearchSafetyCheckListRequest;
import com.smedi.ismedi.safetycheck.dto.response.check.SafetyCheckResponse;
import com.smedi.ismedi.safetycheck.feign.RemoteSafetyCheckFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class SafetyCheckRemoteApiImpl implements SafetyCheckRemoteApi {
    private final RemoteSafetyCheckFeignApi remoteSafetyCheckFeignApi;
    private final AdapterConverter adapterConverter;

    @Override
    public List<SafetyCheckResponse> searchSafetyCheckList(SearchSafetyCheckListRequest searchSafetyCheckListRequest) {
        ResponseData<List<SafetyCheckResponse>> responseData = remoteSafetyCheckFeignApi.searchSafetyCheckListSkipToken(searchSafetyCheckListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }

    @Override
    public List<SafetyCheckVo> getSafetyCheckList(SafetyCheckQuery safetyCheckQuery) {
        SafetyCheckListRequest safetyCheckListRequest = adapterConverter.safetyCheckQueryToSafetyCheckRequest(safetyCheckQuery);
        ResponseData<List<SafetyCheckResponse>> responseData = remoteSafetyCheckFeignApi.getSafetyCheckList(safetyCheckListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.safetyCheckResponseToSafetyCheck(responseData.getData());
    }

    @Override
    public Integer getSafetyCheckCount(SafetyCheckQuery safetyCheckQuery) {
        SafetyCheckListRequest safetyCheckListRequest = adapterConverter.safetyCheckQueryToSafetyCheckRequest(safetyCheckQuery);
        ResponseData<Integer> responseData = remoteSafetyCheckFeignApi.getSafetyCheckCount(safetyCheckListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }
}
